package controllers;

import java.util.List;

import models.LatoForm;
import models.LatoTemplate;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.LazyDynaBean;

import play.Logger;
import play.mvc.Controller;
import utils.DBHelper;
import utils.JsonHelper;
/**
 * This is the Form interface use REST to communicate the client. 
 * 
 * <p>
 * Form interface:
 * <ul>
 *  <li><code>String getForms(String templateId);</code>
 *  <p>route: GET /forms/{userId}
 *  <p>test URL: <a href="http://localhost:9000/forms/1">http://localhost:9000/forms/1</a>
 *  <p>get Form List by templateId, return form list in json format.
 *  <li><code>String getForm(String formId);</code>
 *  <p>route: GET /form/{formId}
 *  <p>test URL: <a href="http://localhost:9000/form/1">http://localhost:9000/form/1</a>
 *  <p>get the Form by formId, return form data in json format.
 *  <li><code>String deleteForm(String formId);</code>
 *  <p>route: DELETE     /form/{formId}
 *  <p style='border:1px solid red;'>test Form:<form ><input type='text' name='formId' value='formId'>
 *  <input type='submit' value='delete'></form></p>
 *  <p>delete the form, if return string length=0 means delete successfully, if return string length>0 means delete fails and string is the err msg.
 *  <li><code>String saveForm(String formJson);</code>
 *   <p>route: POST    /form
 *  <p style='border:1px solid red;'>test Form:<form>formJson:<input type='text' name='formJson' value='formJson'>
 *  <input type='submit' value='save'></form></p>
 *  <p>if templateId>0 means update form, otherwise will create form.
 *  </ul>
 * 
 *  
 * @author Liura
 * @since 2011-4-27
 */

public class LatoFormDelegate extends Controller{	
	/**
	 * get the Form list json
	 * @return  Form list json
	 */
	public static String getForms(String templateId) {		
		List<LatoForm> list = LatoForm.find("byTemplateId", Long.valueOf(templateId)).fetch();
		Logger.info("getForms size:"+list.size());
		JSONArray jsonArr = JSONArray.fromObject(list);
		return jsonArr.toString();
	}
	
	/**
	 * get the Form  json by formId
	 * 
	 * 1.get template json
	 * 2.add data to recordSet node
	 * 
	 * @return  Form  json
	 */
	public static String getForm(String formId) {
		LatoForm form = LatoForm.findById(Long.valueOf(formId));
		Long templateId = form.templateId;
		
		LatoTemplate template = LatoTemplate.findById(templateId);
		String templateJson = template.templateJson;
		DBHelper.printDBstructure();
		DynaBean rootBean = JsonHelper.getLazyDynaBeanFromJson(templateJson);		
		List<DynaBean> tableList = (List<DynaBean>)rootBean.get("tableMapList");
		int i=0;
		for (DynaBean dynaBean : tableList) {
			LazyDynaBean tableBean = new LazyDynaBean();
			
			String tableName = dynaBean.get("tableName").toString();
			List<String> fieldList = DBHelper.getFieldList(tableName);
			
			
			tableBean.set("tableName", tableName);
			
			int fieldCount = 0;
			for (String fieldName : fieldList) {
				tableBean.set("fieldList", fieldCount++, fieldName);
			}
			
			List<List<Object>> recordList = DBHelper.getValueListByFormId(tableName, Long.valueOf(formId));
			String valueStr = JSONArray.fromObject(recordList).toString();
			Logger.info(valueStr);
			tableBean.set("valueList", valueStr);					
			
			rootBean.set("dataTableList", i++, tableBean);
		}	
		
		return JSONObject.fromObject(rootBean).toString();
	}
	
	/**
	 * delete form  by formId
	 */	
	public static String deleteForm(String formId) {
		LatoForm form = LatoForm.findById(Long.valueOf(formId));
		if(form!=null){
			form.delete();
		}		
		return "";
	}
	
	/**
	 * save form   
	 */	
	public static String saveForm(String formJson) {
//		Logger.info("saveForm...formJson:"+formJson);
		DynaBean rootBean = JsonHelper.getDynaBeanFromJson(formJson);//.getDynaBeanFromJson(formJson);
//		System.out.println(rootBean);
		String templateId = (String) rootBean.get("templateId");
		LatoForm form = new LatoForm();
		form.templateId = Long.parseLong(templateId);
		form.save();
		System.out.println("form.id="+form.id);
		List dataBeanList = (List) rootBean.get("dataTableList");
		for (Object object : dataBeanList) {
			DynaBean dataBean = (DynaBean)object;
			System.out.println(dataBean);
//			dataBean.set("formId", form.id);
			DBHelper.saveData(dataBean, form.id);
		}
		
		
		return "saveForm json";
	}

}
